// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------
// Modification history:
// feature: Unified Transport Abstraction Layer Unified Transport Abstraction Layer
// ------------------------------------------------------------------

#ifndef INCLUDE_VBS_TRANSPORT_TCPV4TRANSPORTDESCRIPTOR_H_
#define INCLUDE_VBS_TRANSPORT_TCPV4TRANSPORTDESCRIPTOR_H_

#include <sstream>

#include <transport/TCPTransportDescriptor.h>
#include <deps/common/Types.h>

namespace vbs {
namespace transport {

/**
 * TCPv4 Transport configuration.
 * The kind value for TCPv4TransportDescriptor is given by \c evbs::ertps::rtps::LOCATOR_KIND_TCPv4.
 *
 * - \c wan_addr: Public IP address. Peers on a different LAN will use this IP for communications
 * with this host.
 *
 * @ingroup TRANSPORT_MODULE
 */
struct TCPv4TransportDescriptor : public TCPTransportDescriptor {
    //! Destructor
    virtual ~TCPv4TransportDescriptor() = default;

    TransportInterface* create_transport() const override;

    TransmitorType get_type() const override;
    //! Public IP address
    unsigned char wan_addr[4];

    //! Set the public IP address
    void set_WAN_address(unsigned char o1, unsigned char o2, unsigned char o3, unsigned char o4) {
        wan_addr[0] = o1;
        wan_addr[1] = o2;
        wan_addr[2] = o3;
        wan_addr[3] = o4;
    }

    //! Set the public IP address
    void set_WAN_address(const std::string& in_address) {
        std::stringstream ss(in_address);
        int a, b, c, d;  // to store the 4 ints
        char ch;         // to temporarily store the '.'
        try {
            ss >> a >> ch >> b >> ch >> c >> ch >> d;
        } catch (const std::invalid_argument& e) {
            // stoi threw an std::invalid_argument exception.
            std::cout << "Invalid argument: " << e.what() << std::endl;
            return;
        } catch (const std::out_of_range& e) {
            // stoi threw an std::out_of_range exception.
            std::cout << "Out of range: " << e.what() << std::endl;
            return;
        } catch (const std::exception& e) {
            // stoi threw an std::out_of_range exception.
            std::cout << "Other exception: " << e.what() << std::endl;
            return;
        }
        wan_addr[0] = (unsigned char)a;
        wan_addr[1] = (unsigned char)b;
        wan_addr[2] = (unsigned char)c;
        wan_addr[3] = (unsigned char)d;
    }

    //! Get the public IP address
    std::string get_WAN_address() {
        std::stringstream ss;
        ss << static_cast<int>(wan_addr[0]) << "." << static_cast<int>(wan_addr[1]) << "."
           << static_cast<int>(wan_addr[2]) << "." << static_cast<int>(wan_addr[3]);
        return ss.str();
    }

    //! Constructor
    RTPS_DllAPI TCPv4TransportDescriptor();

    //! Copy constructor
    RTPS_DllAPI TCPv4TransportDescriptor(const TCPv4TransportDescriptor& t);

    //! Copy assignment
    RTPS_DllAPI TCPv4TransportDescriptor& operator=(const TCPv4TransportDescriptor& t);

    //! Comparison operator
    RTPS_DllAPI bool operator==(const TCPv4TransportDescriptor& t) const;
};

}  // namespace transport
}  // namespace vbs

#endif  // INCLUDE_VBS_TRANSPORT_TCPV4TRANSPORTDESCRIPTOR_H_
